package com.xlh.dao.conclude.helper;

import cn.hutool.core.collection.CollectionUtil;
import com.xlh.dao.conclude.ConcludeCourseMapper;
import com.xlh.pojo.conclude.ConcludeCourse;
import com.xlh.pojo.conclude.example.ConcludeCourseExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ConcludeCourseMapperHelper {

    @Autowired
    private ConcludeCourseMapper concludeCourseMapper;


    public ConcludeCourse get(Long id){

        ConcludeCourseExample concludeCourseExample = new ConcludeCourseExample();
        concludeCourseExample.createCriteria().andDeletedEqualTo(false).andIdEqualTo(id);
        List<ConcludeCourse> concludeCourses = concludeCourseMapper.selectByExample(concludeCourseExample);

        if(CollectionUtil.isNotEmpty(concludeCourses)){
            return concludeCourses.get(0);
        }
        return null;
    }

    public List<ConcludeCourse> findByCondition(Long concludeId){

        ConcludeCourseExample concludeCourseExample = new ConcludeCourseExample();
        concludeCourseExample.createCriteria().andDeletedEqualTo(false).andConcludeIdEqualTo(concludeId);
        concludeCourseExample.setOrderByClause("`order` asc");
        List<ConcludeCourse> concludeCourses = concludeCourseMapper.selectByExample(concludeCourseExample);

        return concludeCourses;
    }


    /**
     * 根据目标ID删除相关课程
     * @param concludeId
     */
    public void delete(Long concludeId) {

        ConcludeCourse record = new ConcludeCourse();
        record.setDeleted(true);
        ConcludeCourseExample concludeCourseExample = new ConcludeCourseExample();
        concludeCourseExample.createCriteria().andConcludeIdEqualTo(concludeId);
        concludeCourseMapper.updateByExampleSelective(record, concludeCourseExample);
    }

    /**
     * 批量删除
     */
    public void batchDeleted(List<Long> ids){

        ConcludeCourse record = new ConcludeCourse();
        record.setDeleted(true);
        ConcludeCourseExample concludeCourseExample = new ConcludeCourseExample();
        concludeCourseExample.createCriteria().andIdIn(ids).andDeletedEqualTo(false);
        concludeCourseMapper.updateByExampleSelective(record, concludeCourseExample);
    }

    /**
     * 批量更新课程相关数据
     * @param upConcludeCourses
     */
    public void batchUpdate(List<ConcludeCourse> upConcludeCourses) {

        for (ConcludeCourse upConcludeCours : upConcludeCourses) {
            ConcludeCourseExample concludeCourseExample = new ConcludeCourseExample();
            concludeCourseExample.createCriteria().andDeletedEqualTo(false).andIdEqualTo(upConcludeCours.getId());
            upConcludeCours.setId(null);  // 更新设置为空
            concludeCourseMapper.updateByExampleSelective(upConcludeCours, concludeCourseExample);
        }
    }


    /**
     * 插入数据
     * @param concludeCours
     */
    public void insert(ConcludeCourse concludeCours) {

        concludeCourseMapper.insert(concludeCours);
    }
}